package cn.dengta.webapp.trade.service;

import cn.dengta.webapp.base.service.BaseServiceImpl;
import cn.dengta.webapp.trade.dao.PhoneBlackListDao;
import cn.dengta.webapp.user.entity.PhoneBlackList;
import cn.dengta.webapp.user.entity.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

/**
 * 手机号黑名单 服务实现类
 *
 * @author lizhichao
 * @since 2024-06-26
 */
@Service
public class PhoneBlackListService extends BaseServiceImpl<PhoneBlackList, PhoneBlackListDao, Long> {

	public boolean isBlackList(String phone) {
		return exists(PhoneBlackList.PHONE, phone);
	}

	public boolean isBlackList(Long userId) {
		return exists(PhoneBlackList.USER_ID, userId);
	}

	public void addBlackList(User input) {
		final String phone = input.getPhone();
		PhoneBlackList one = getOne(phone);
		if (one == null) {
			return;
		}
		PhoneBlackList blackList = new PhoneBlackList();
		blackList.setUserId(input.getId());
		blackList.setPhone(phone);
		blackList.setRemark(one.getRemark());
		save(blackList);
	}

	public PhoneBlackList getOne(String phone) {
		QueryWrapper<PhoneBlackList> wrapper = Wrappers.query();
		wrapper.eq(PhoneBlackList.PHONE, phone)
				.last("limit 1");
		return selectOne(wrapper);
	}

}
